const db = require('../../lib/conn_oracle')

exports.getWorklogInfo = async (req,res)=>{
    let {form} = req.body
    let {date,status,desc} = form
    // console.log(req.body);
    // console.log(date);
    
    let resDatas     
    let statusWhere = status === "" ? "" : `and status = '${status}'`
    let descWhere = desc === "" ? "" : `and description like '%${desc}%'`
    let handleSql = `
                     select * from c##wxw.worklog
                     where schedfinish >= trunc(to_timestamp('${date}','yyyy-mm-dd hh24:mi:ss.ff'),'mm')
                       and schedfinish <= add_months(trunc(to_timestamp('${date}','yyyy-mm-dd hh24:mi:ss.ff'),'mm'),+1)-1
                       ${statusWhere}
                       ${descWhere}
                       order by b_type,s_type,description,schedfinish
                    `
    let workTypeSql = `select * from c##wxw.worktype order by b_type,s_type`
    let worklogInfo = await db.search2(handleSql)
    let workTypeInfo = await db.search2(workTypeSql)
    resDatas = {worklogInfo,workTypeInfo}
    res.json({
        status:1,
        msg:"查询成功",
        resDatas
    })
}

exports.editWorklog = async (req,res)=>{
    // console.log(req.body);
    let {type,form} = req.body
    let {ID,DESCRIPTION,STATUS,SCHEDFINISH,ACTFINISH,LONG_DESC,REMARK,B_TYPE,S_TYPE} = form
    // ID = Number(ID)
    let resDatas
    let msg
    let status
    let submitTypeStr = type === "add" ? "新增" : "更新"
    let handleSql = type === "add" ? `
                                      insert into c##wxw.worklog(id,description,status,schedfinish,actfinish,long_desc,remark,b_type,s_type)
                                      values('',:DESCRIPTION,:STATUS,to_timestamp(:SCHEDFINISH,'yyyy-mm-dd hh24:mi:ss.ff'),to_timestamp(:ACTFINISH,'yyyy-mm-dd hh24:mi:ss.ff'),:LONG_DESC,:REMARK,:B_TYPE,:S_TYPE)
                                     `
                                    :  `
                                        update c##wxw.worklog
                                        set 
                                        description = :DESCRIPTION,
                                        status = :STATUS,
                                        schedfinish = to_timestamp(:SCHEDFINISH,'yyyy-mm-dd hh24:mi:ss.ff'),
                                        actfinish = to_timestamp(:ACTFINISH,'yyyy-mm-dd hh24:mi:ss.ff'),
                                        long_desc = :LONG_DESC,
                                        remark = :REMARK,
                                        b_type = :B_TYPE,
                                        s_type = :S_TYPE
                                        where id = :ID
                                        `
    let handleDate = type === "add" ? [DESCRIPTION,STATUS,SCHEDFINISH,ACTFINISH,LONG_DESC,REMARK,B_TYPE,S_TYPE] 
                                    : [DESCRIPTION,STATUS,SCHEDFINISH,ACTFINISH,LONG_DESC,REMARK,B_TYPE,S_TYPE,ID]
    // console.log(  handleDate  );
    
    let newWorklog = await db.writeTab(handleSql,handleDate)
    resDatas = {newWorklog}
    msg = resDatas.newWorklog.errorNum ? `${submitTypeStr}失败，错误代码：ORA-` + resDatas.newWorklog : `${submitTypeStr}成功`
    status = resDatas.newWorklog.errorNum ? 0 : 1
    res.json({
        status,
        msg,
        resDatas
    })
                                                           
}